package com.liugui.genericsandcollections;

import java.util.*;

/**
 * @ClassName: ListTest
 * @Author: liugui
 * @Date: 2020-11-04 11:12
 **/
public class ListTest {
    private List<Student> studentList;

    public ListTest() {
        this.studentList = new ArrayList<>();
    }

    /**
     * 添加学生
     */
    public void testAdd(){
        Student student = new Student("1","liugui");
        //添加元素
        studentList.add(student);

        //取出第一个元素 索引0
        Student temp = studentList.get(0);
        System.out.println("学生ID：" + temp.id + "学生姓名：" + temp.name);

        Student student1 = new Student("2","longtu");
        //在索引0的位置插入学生
        studentList.add(0,student1);
        temp = studentList.get(0);
        System.out.println("学生ID：" + temp.id + "学生姓名：" + temp.name);
        temp = studentList.get(1);
        System.out.println("学生ID：" + temp.id + "学生姓名：" + temp.name);
        // 对象数组的形式添加
        Student[] students = {new Student("3", "王五"), new Student("4", "马六")};

        // Arrays类包含用来操作数组（比如排序和搜索）的各种方法，asList() 方法用来返回一个受指定数组支持的固定大小的列表
        studentList.addAll(Arrays.asList(students));
        Student temp3 = studentList.get(2);
        Student temp4 = studentList.get(3);
        System.out.println("添加了学生：" + temp3.id + ":" + temp3.name);
        System.out.println("添加了学生：" + temp4.id + ":" + temp4.name);
        Student[] student2 = {new Student("5", "周七"), new Student("6", "赵八")};
        studentList.addAll(2, Arrays.asList(student2));
        Student temp5 = studentList.get(2);
        Student temp6 = studentList.get(3);
        System.out.println("添加了学生：" + temp5.id + ":" + temp5.name);
        System.out.println("添加了学生：" + temp6.id + ":" + temp6.name);

    }

    /**
     * 取得List中的元素的方法
     */
    public void testGet() {
        int size = studentList.size();
        for (int i = 0; i < size; i++) {
            Student st = studentList.get(i);
            System.out.println("学生：" + st.id + ":" + st.name);
        }
    }


    /**
     * 迭代器遍历
     */
    public void testIterator(){
        System.out.println("有如下学生（通过Iterator）：");
        Iterator<Student> iterator = studentList.iterator();
        while (iterator.hasNext()){
            Student st = iterator.next();
            System.out.println("学生：" + st.id + ":" + st.name);
        }
    }


    /**
     * 通过for each 方法访问集合元素
     *
     */
    public void testForEach() {
        System.out.println("有如下学生（通过for each）：");
        for (Student obj : studentList) {
            System.out.println("学生：" + obj.id + ":" + obj.name);
        }
        //使用java8 Steam将学生排序后输出
        studentList.stream()//创建Stream
                //通过学生id排序
                .sorted(Comparator.comparing(x -> x.id))
                //输出
                .forEach(System.out::println);
    }


    /**
     * 修改List中的元素
     *
     */
    public void testModify() {
        studentList.set(4, new Student("3", "吴酒"));
    }



    /**
     * 删除List中的元素
     *
     */
    public void testRemove() {
        Student st = studentList.get(4);
        System.out.println("我是学生：" + st.id + ":" + st.name + "，我即将被删除");
        studentList.remove(st);
        System.out.println("成功删除学生！");
        testForEach();
    }


    public static void main(String[] args) {
        ListTest listTest = new ListTest();
        listTest.testAdd();
        listTest.testIterator();
        listTest.testForEach();

        listTest.testModify();
        listTest.testRemove();
    }
}
